package cn.sinjet.carassist;

import java.util.ArrayList;
import java.util.List;

import com.mgroup.wazehelper.R;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import cn.sinjet.adapter.MyObdListAdapter;
import cn.sinjet.database.DAOFactory;
import cn.sinjet.database.OBDDAO;
import cn.sinjet.viewmodel.IUpdateUI;
import cn.sinjet.viewmodel.PageDefine;
import cn.sinjet.viewmodel.ViewModel;

public class ErrQueryActivity extends MyActivity implements OnClickListener,IUpdateUI{
	SinjetApplication app;
	Button btnBack;
	ListView listViewOBD;
	EditText vOBDSearchInput;
	List<String> listOBDData = new ArrayList<String>();
	MyObdListAdapter listAdapterOBD;
	String m_strSearchInput = "";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setMyContentView(R.layout.activity_err_query);
		app = SinjetApplication.getInstance();
//		LayoutInflater inflater = LayoutInflater.from(this);
//		ViewGroup mViewRoot = (ViewGroup)inflater.inflate(R.layout.activity_err_query,null);
//		setContentView(mViewRoot);
       
		btnBack = (Button)mViewRoot.findViewById(R.id.err_query_back);
		btnBack.setOnClickListener(this);
		
		listViewOBD = (ListView)mViewRoot.findViewById(R.id.obd_search_listview_obd);
		listAdapterOBD = new MyObdListAdapter(this,listOBDData);
		listViewOBD.setAdapter(listAdapterOBD);
		new Handler().postDelayed(loadOBDDataRunnable, 50);
		
		vOBDSearchInput =  (EditText) mViewRoot.findViewById(R.id.obd_search_et_input);
		vOBDSearchInput.setOnKeyListener(onKeyListener);
		
		mViewRoot.findViewById(R.id.obd_search_bt_cancel_search).setOnClickListener(this);
	}
	
	Runnable loadOBDDataRunnable = new Runnable() {
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			loadOBDData();
		}
	};
	
	
	private void  loadOBDData(){
		OBDDAO obdDao = DAOFactory.getInstance().getOBDDao();
		if(obdDao == null)
			return;
		listOBDData = obdDao.getObdItemLike("");
		listAdapterOBD.setListData(listOBDData);
	}
	
	OnKeyListener onKeyListener = new OnKeyListener() {

		public boolean onKey(View v, int keyCode, KeyEvent event) {
			if (KeyEvent.KEYCODE_ENTER == keyCode
					&& event.getAction() == KeyEvent.ACTION_DOWN) {

				m_strSearchInput = vOBDSearchInput.getText().toString()
						.trim();

				// big or small letter process
				String strRet = "";
				int i = 0;
				for (i = 0; i < m_strSearchInput.length(); i++) {
					char c = m_strSearchInput.charAt(i);
					boolean b = Character.isLowerCase(c);
					if (b == true)
						strRet += Character.toUpperCase(c);
					else
						strRet += c;
				}
				m_strSearchInput = strRet;
				// ///////////////////////////////////////////////////
				closeInputMethod();
				OBDDAO obdDao = DAOFactory.getInstance().getOBDDao();
				if(obdDao == null)
					return false;
				listOBDData = obdDao.getObdItemLike(m_strSearchInput);
				listAdapterOBD.setListData(listOBDData);
				return true;
			}
			return false;
		}
	};
	
	private void closeInputMethod() {
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		boolean isOpen = imm.isActive();
		if (isOpen) {
			// imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
			View view = getWindow().peekDecorView();
			imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
		}
	}
	
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch(view.getId()){
		case R.id.err_query_back:
			ViewModel.getIns().jumpPage(PageDefine.PAGE_CAR_CONDITION);
			break;
		case R.id.obd_search_bt_cancel_search:{
			vOBDSearchInput.setText("");
			closeInputMethod();
//			if (listOBDData.size() == 0)
				new Handler().postDelayed(loadOBDDataRunnable, 50);
		   }break;
		}
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		ViewModel.getIns().onActivityResume(this, PageDefine.PAGE_ERR_QUERY, this);
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		finish();
	}
	
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode == KeyEvent.KEYCODE_BACK){
			ViewModel.getIns().jumpPage(PageDefine.PAGE_CAR_CONDITION);
			return true;
		}
		return super.onKeyUp(keyCode, event);
	}

}
